//index.js
var db = require('../../utils/db.js')
var qr = require('../../utils/qrcode')


//获取应用实例
var app = getApp()
Page({
    data: {
        motto: 'Hello World',
        list: [],
        current:0,
        indicatorDots: true
    },
    //事件处理函数
    bindViewTap: function () {
        wx.navigateTo({
            url: '../logs/logs'
        })
    },
    onShow: function () {
      this.flushList()
    },
    flushList: function (){
      var list  = db.getList();
      for(var i in list){
        console.log(db.toText(list[i]))
        list[i].qrcode = qr.createQrCodeImg(db.toText(list[i]), { size: 150 });
      }
      this.setData({
        list: list,
      })
    },
    onShareAppMessage: function (res) {
      if (res.from === 'button') {
        // 来自页面内转发按钮
        console.log(res.target)
      }
      return {
        title: '超级好用的发票助手!',
        path: '/pages/index/index?id=_share',
        success: function (res) {
          // 转发成功
        },
        fail: function (res) {
          // 转发失败
        }
      }
    },
    deleteItem: function (e) {

        var that = this
        wx.showModal({
          title: '提示',
          content: '确认要删除吗?',
          success: function (res) {
            if (res.confirm) {
              console.log("remove")
              var id = e.currentTarget.dataset.id
              console.log(id)
              db.deleteItem(id)
                that.setData({
                    current:0
                })
              that.flushList()
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
    },
    addItem: function () {
        wx.navigateTo({
            url: '../add/add'
        })
    },
    onLoad: function (options) {
        console.log('onLoadonLoad')
        console.log(options)
        var that = this
        var id = 0
        if (options.hasOwnProperty('id')){
            id = options['id'] ;
        }
        that.setData({
            current: id
        })
    }
})
